package com.cictec.middleware.distribute.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.Result;
import com.cictec.middleware.commons.utils.ResultUtil;
import com.cictec.middleware.core.entity.BusDriver;
import com.cictec.middleware.core.pojo.MiddlewareDriverSign;
import com.cictec.middleware.feign.dc.MiddlewareDcDriverServiceFeign;
import com.cictec.middleware.feign.gps.MiddlewareGPSService;
import com.cictec.middleware.ribbon.aspect.ThreadContextHolder;

/**
 * 中间件对外业务处理接口【司机签到业务 司机相关业务】
 * 
 * @author Administrator
 *
 */
@RestController
public class MiddlewareDriverController {

	@Autowired
	private MiddlewareGPSService middlewareGPSService;
	@Autowired
	private MiddlewareDcDriverServiceFeign middlewareDcDriverServiceFeign;
	
	
	
	/**
	 * 设备上传 司机签到
	 * 
	 * @param devCode
	 *            设备编号
	 * @param icCardNumber
	 *            IC卡号
	 * @param signTime
	 *            签到时间
	 * @return
	 */
	@RequestMapping(value = "/distribute/busDriver/driverSign")
	public Result driverSign(@RequestBody MiddlewareDriverSign middlewareDriverSign) {
		
		String devCode = middlewareDriverSign.getDevCode();
		String icCardNumber = middlewareDriverSign.getCardNumber();
		String cityCode = middlewareDriverSign.getCityCode();
		Date signTime = middlewareDriverSign.getSignTime();
		
		ThreadContextHolder.setValue(devCode, cityCode);// 目前先通过代码侵入方式实现，后期在优化
		
		// 数据入库
		BusDriver driver = middlewareDcDriverServiceFeign.getDriverByIcCardNumber(icCardNumber);
		if(driver != null){
			middlewareDcDriverServiceFeign.driverSign(middlewareDriverSign);
			middlewareGPSService.refreshBusDriver(devCode, icCardNumber, signTime);
			System.out.println("签到时间："+ signTime);
			
			return ResultUtil.getSuccessResult(driver);
		}
		return ResultUtil.getErrorResult("找不到序列号:{} 对应的司机", icCardNumber);
		// 通知GPS服务刷新缓存
		
		
	}
}
